package edu.example;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class editDialogInCreateAssignments extends JDialog{

	// GUI
	private JLabel nameLabel;
	private JCheckBox selectCheckBox;
	private JCheckBox availableCheckBox;
	private JLabel workloadLabel;
	private JTextField workloadTextField;
	private JButton okButton;
	private JButton cancelButton;
	
	// handler
	private OkHandler okHandler;
	private CancelHandler cancelHandler;
	
	// data control
	private CreateReviewFrame crf;
	private Reviewer rToEdit;
	private int index;
	private boolean select;
	
	public editDialogInCreateAssignments(CreateReviewFrame initCrf, Reviewer initR, int initIndex, boolean initSelect){
		crf = initCrf;
		rToEdit = initR;
		index = initIndex;
		select = initSelect;
		layoutGUI();
	}
	
	public void layoutGUI(){
		this.setTitle("Edit");
		nameLabel = new JLabel(rToEdit.getName());
		selectCheckBox = new JCheckBox("Selected");
		selectCheckBox.setSelected(select);
		availableCheckBox = new JCheckBox("Available");
		if (rToEdit.getAvailable())
			availableCheckBox.setSelected(true);
		workloadLabel = new JLabel("Workload: ");
		workloadTextField = new JTextField(""+rToEdit.getWorkload());
		okButton = new JButton("OK");
		cancelButton = new JButton("Cancel");
		
		okHandler = new OkHandler();
		cancelHandler = new CancelHandler();
		okButton.addActionListener(okHandler);
		cancelButton.addActionListener(cancelHandler);
		
		
		GridBagLayout layout = new GridBagLayout();
		this.setLayout(layout);
		addComponent(nameLabel,					this,	0,	0,	1,	1);
		addComponent(selectCheckBox,			this,	1,	0,	1,	1);
		addComponent(availableCheckBox,			this,	2,	0,	1,	1);
		addComponent(workloadLabel,				this,	3,	0,	1,	1);
		addComponent(workloadTextField,			this,	4,	0,	1,	1);
		addComponent(okButton,					this,	3,	1,	1,	1);
		addComponent(cancelButton,				this,	4,	1,	1,	1);
		
		// size and locate the dialog at the center of the screen
		this.pack();
		this.setModal(true);
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	class OkHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			boolean select2 = selectCheckBox.isSelected();
			boolean available = availableCheckBox.isSelected();
			String workload = workloadTextField.getText();
			
			if (!available && select2){
				JOptionPane.showMessageDialog(editDialogInCreateAssignments.this, 
						"Error: Cannot select the one who is not Available", "Error", JOptionPane.ERROR_MESSAGE);
			}
			else {
				rToEdit.setAvailable(available);
				rToEdit.setWorkload(Float.parseFloat(workload));
				crf.update(rToEdit, index);
				crf.setSelected(rToEdit, index, select2);
				
				editDialogInCreateAssignments.this.setVisible(false);
			}
		}
		
	}
	
	class CancelHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			editDialogInCreateAssignments.this.setVisible(false);
		}
		
	}
	
}
